package com.juqimiao.raccoon.commodity.controller;

import com.juqimiao.raccoon.commodity.appobj.CategoryGroupListAO;
import com.juqimiao.raccoon.commodity.dto.AddCategoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.SortCatgoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.UpdateCategoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.query.ListCategoryGroupQry;
import com.juqimiao.raccoon.commodity.dto.SwitchCategoryGroupPublishCmd;
import com.juqimiao.raccoon.commodity.service.CategoryGroupServiceI;
import com.juqimiao.linn.transfer.Response;
import com.juqimiao.linn.transfer.query.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Collin Cai
 */
@RestController
@RequestMapping("/cate")
public class CategoryController {

    @Autowired
    private CategoryGroupServiceI categoryGroupService;

    /**
     * If expose as rest api, it Should be use {@link ResponseEntity}, not {@link Response}.
     * Otherwise, if use RPC, then consider use {@link Response} as return type directly.
     */
    @PostMapping("/group/create")
    public Response create(@RequestHeader HttpHeaders headers,
                                @RequestBody AddCategoryGroupCmd addCategoryGroupCmd) {

        //仅仅只是为了演示，一般不会如此获取一个userId。
        // 通过解析Bear Token解析一个userId，是一般化途径。
        List<String> list = headers.get("userId");
        String userId = list.get(0);
        //Get Tenant and request information by tenantId and userId, and set the Linn Context in the interceptor.
        return categoryGroupService.createGroup(userId, addCategoryGroupCmd);
    }

    @PutMapping("/group/public")
    public Response publish(@RequestHeader HttpHeaders headers,
                            @RequestBody SwitchCategoryGroupPublishCmd cmd) {
        //仅仅只是为了演示，一般不会如此获取一个userId。
        List<String> list = headers.get("userId");
        String userId = list.get(0);
        return categoryGroupService.publishGroup(userId, cmd);
    }

    @PutMapping("/group/sort")
    public Response sort(@RequestHeader HttpHeaders headers,
                         @RequestBody SortCatgoryGroupCmd cmd) {
        List<String> list = headers.get("userId");
        String userId = list.get(0);
        return categoryGroupService.sort(cmd);
    }

    @PutMapping("/group/update")
    public Response update(@RequestHeader HttpHeaders headers,
                           @RequestBody UpdateCategoryGroupCmd cmd) {
        //仅仅只是为了演示，一般不会如此获取一个userId。
        List<String> list = headers.get("userId");
        String userId = list.get(0);
        return categoryGroupService.updateGroup(userId, cmd);
    }

    @GetMapping("/group/list")
    public QueryResponse<List<CategoryGroupListAO>> list(@RequestHeader HttpHeaders headers,
                                                         @RequestBody ListCategoryGroupQry listCategoryGroupQry) {
        //仅仅只是为了演示，一般不会如此获取一个userId。
        List<String> list = headers.get("userId");
        String userId = list.get(0);
        return categoryGroupService.list(userId, listCategoryGroupQry);
    }
}
